package com.example.springbootrabbitmq.consumer;

import com.example.springbootrabbitmq.entity.TOrder;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author HL
 */
@Component
public class OrderConsumer {

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "order-queue",durable = "true"),
            exchange = @Exchange(value = "order-exchange", durable = "true", type = "topic"),
            key = "order.#"))
    @RabbitHandler
    public void onOrderMessage(@Payload TOrder tOrder, @Headers Map<String, Object> headers, Channel channel) throws Exception {
        System.out.println("---------------------收到消息----------------------");
        System.out.println("订单ID：" + tOrder.getId());
        long deliveryTag = (long) headers.get(AmqpHeaders.DELIVERY_TAG);

        channel.basicAck(deliveryTag, false);
    }
}
